package com.pandora.credit.service.impl;

import com.pandora.common.util.Constants;
import com.pandora.common.util.JedisUtil;
import com.pandora.common.util.StringUtils;
import com.pandora.common.util.VerifyUtil;
import com.pandora.credit.dao.HistoryDao;
import com.pandora.credit.dto.request.HistoriesRequest;
import com.pandora.credit.entity.Credit;
import com.pandora.credit.entity.History;
import com.pandora.credit.exception.auth.AuthorizationTokenInvalidException;
import com.pandora.credit.exception.customer.CustomerNotFoundException;
import com.pandora.credit.service.CommonService;
import com.pandora.credit.service.CreditService;
import com.pandora.credit.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author developer
 */
@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryDao historyDao;
    @Autowired
    private CommonService commonService;
    @Autowired
    private CreditService creditService;

    @Override
    public List<History> selectByUpdateTime(long custId, int type, String updateTime, int pageLength) {
        return historyDao.queryByUpdateTime(custId, type, updateTime, pageLength);
    }

    @Override
    public History selectByCusIdTypeDoc(long cusId, int type, long docId) {
        return historyDao.queryByCusIdTypeDoc(cusId, type, docId);
    }

    @Override
    public void save(History history) {
        historyDao.insert(history);
    }

    @Override
    public void update(History history) {
        historyDao.update(history);
    }

    @Override
    public Object doPostHistories(String authorizationToken, HistoriesRequest req) {
        String mobile = JedisUtil.get(Constants.BUSINESS_REDIS_URL,
                StringUtils.getStrParam(Constants.AUTHORIZATION_TOKEN_KEY, authorizationToken));
        if(VerifyUtil.isEmpty(mobile)){
            throw new AuthorizationTokenInvalidException();
        }
        long cusId = commonService.getCustomerIdByMobile(mobile);
        if(cusId < 0){
            throw new CustomerNotFoundException();
        }
        int dbType = req.getType();
        long docId = req.getId();
        if(dbType <= 0 || docId <= 0){
            return -1;
        }

        History resHistory = selectByCusIdTypeDoc(cusId, dbType, docId);
        long nowDate = System.currentTimeMillis();
        if(resHistory == null){
            resHistory = new History();
            resHistory.setId(Long.parseLong(StringUtils.seqNoGeneratorNextId(Constants.SEQ_NO_GENERATORS_TS)));
            resHistory.setCustId(cusId);
            resHistory.setType(dbType);
            resHistory.setDocId(docId);
            resHistory.setClickCount(1);
            resHistory.setCreateTime(new Timestamp(nowDate));
            resHistory.setUpdateTime(new Timestamp(nowDate));
            save(resHistory);

            Credit find = new Credit();
            find.setId(docId);
            List<Credit> credits = creditService.getByCondition(find);
            if(credits.size() > 0){
                Credit credit = credits.get(0);
                credit.setAppCount(credit.getAppCount() + 1);
                credit.setUpdateTime(new Timestamp(nowDate));
                creditService.update(credit);
            }
        }
        else {
            int clickCount = resHistory.getClickCount() + 1;
            resHistory.setClickCount(clickCount);
            resHistory.setUpdateTime(new Timestamp(nowDate));
            update(resHistory);
        }
        return null;
    }
}
